package gold.digger;

import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC98 {


    public long preVal = Long.MIN_VALUE;

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }

        if (root.left != null && root.left.val >= root.val) {
            return false;
        }

        if (root.right != null && root.right.val <= root.val) {
            return false;
        }

        boolean left = isValidBST(root.left);
        if (!left) {
            return false;
        }

        if (root.val <= preVal) {
            return false;
        }

        //update preVal
        preVal = root.val;

        boolean right = isValidBST(root.right);
        if (!right) {
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        LC98 an = new LC98();
        System.out.println(an.isValidBST(null));
        ;
    }
}
